package com.quwan.common.service.impl.ums;

import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import com.quwan.common.enums.EnvironmentEnum;
import com.quwan.common.enums.StatusEnum;
import com.quwan.common.form.JpushForm;
import com.quwan.common.form.PushForm;
import com.quwan.common.result.PushDTO;
import com.quwan.common.service.push.MsgSender;
import com.quwan.config.yml.PushConfig;
import com.quwan.utils.JpushUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class AndroidUMSMessage implements MsgSender {

    @Override
    public PushDTO pushAll(PushForm pushForm, PushConfig pushConfig) {
        JpushUtil.createClient(pushConfig.getJums().getAppKey(), pushConfig.getJums().getMasterSecret());
        JpushForm form = new JpushForm(pushForm.getTitle(),
                pushForm.getContent(),pushForm.getContent(),
                pushForm.getExtrasMap(),
                pushConfig.getJums().getEnvironment());
        PushResult pushResult = JpushUtil.pushAndroidAndIos(form);
        if (pushResult !=null){
            if (pushResult.isResultOK()){
                return new PushDTO(String.valueOf(pushResult.msg_id),StatusEnum.SUCEESS.getCode());
            }
        }
        return new PushDTO("",StatusEnum.FAIL.getCode());
    }

    @Override
    public PushDTO pushToken(PushForm pushForm, PushConfig pushConfig) {
        log.info("推送别名：" + pushForm.getAlias());
        JpushUtil.createClient(pushConfig.getJums().getAppKey(), pushConfig.getJums().getMasterSecret());
        JpushForm form = new JpushForm(pushForm.getAlias(),pushForm.getTitle(),
                pushForm.getContent(),pushForm.getContent(),
                pushForm.getExtrasMap(),Platform.android(),
                pushConfig.getJums().getEnvironment());
        PushResult pushResult = JpushUtil.pushAliasAlertWithTitle(form);
        if (pushResult != null){
            if (pushResult.isResultOK()){
                return new PushDTO(String.valueOf(pushResult.msg_id),StatusEnum.SUCEESS.getCode());
            }
        }
        return new PushDTO("",StatusEnum.FAIL.getCode());
    }


    public static void main(String[] args) {

        JpushUtil.createClient("c3bcfea57720148c69f6f862","709bd9f324b3a8e3aca01f35");
        Map map = new HashMap();
        map.put("orderId","123456ios2");
//        JpushForm form = new JpushForm("测试","下单测试","下单测试",map, (byte) 1);
        JpushForm form = new JpushForm("1505869737834389505","测试ios2","下单测试ios2","下单测试ios2",map,Platform.ios(), (byte) 1);
        PushResult pushResult = JpushUtil.pushAliasAlertWithTitle(form);
        log.info("pushResult:" + (pushResult != null));
    }
}
